package com.fd.servlet;

import com.fd.entity.Student;
import com.fd.service.IStudentService;
import com.fd.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class AddServlet extends HttpServlet {
//    @Autowired
//    private StudentService studentService;
//    StudentService studentService = new StudentService();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("applicationContext-jdbc-ann.xml");
        StudentService studentService = classPathXmlApplicationContext.getBean(StudentService.class);


        Student newStudent = new Student(request.getParameter("name"), Integer.valueOf(request.getParameter("age")), request.getParameter("gender"), request.getParameter("address"), Integer.valueOf(request.getParameter("classes")));
        studentService.add(newStudent);
        request.setAttribute("student", studentService.getAll());
        request.getRequestDispatcher("WEB-INF/html/data.jsp").forward(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}